var appPackageName = "com.xingin.xhs";
var deviceWidth = device.width;
var deviceHeight = device.height;

sleep(500)
app.launchPackage(appPackageName);
if (!requestScreenCapture()) {
    toast("请求截图失败");
    exit()
}
sleep(2000)

textEndsWith(" 回复").find().forEach(function (target1, index1) {
    if (index1 > 1) {
        return
    }
    let texta = target1.text();
    // 可能存在空元素
    if (target1) { 
        // 评论的内容
        let contStr = String(texta);
        log(contStr)
        // 评论的位置
        let bounds = target1.bounds();
        let anquanqu = deviceHeight - 200;
        // 私信流程
        // if (Boolean(storage.get("isSixin", false))) {
            // if (contStr.match(sixinReg)) {
            //     let str = storage.get("sixinCont", "诚信来互有赞必回随迟必到，欢迎来互！");

            // let bounds1 = target1.parent().parent().findOne(id("dz5")).bounds();
            //     log(bounds1)

            target1.parent().parent().findOne(id("dz5")).click();
            sleep(1000)
            desc("发消息").click();
            sleep(1000)
            className("android.widget.EditText").findOne().click();
            sleep(1000)
            className("android.widget.EditText").findOnce().setText("接");
            sleep(1500)
            id("emw").depth(9).findOnce().click();
            sleep(1000)
            id("emw").depth(9).findOnce().parent().click();
            sleep(1000)
            while ( !id("ww").exists() ) {
                back()
                sleep(1000)
            }
            

            
            // }
        // }
    }
})



/**
 * 抖音搜索 
 * @param {*} text 内容 输入的文字
 */
 function inputHs(text) {
    className("android.widget.EditText").findOne().click();
    waitTime(1, "进入输入框页，准备输入文字");
    className("android.widget.EditText").findOne().setText(text);
    sleep(1500)
    log("停留在这里,说明id发生了变化,手动点点击输入框试试");
    var times = 0;
    // eslint-disable-next-line no-constant-condition
    while (true) {
        if (className("android.widget.TextView").text("发送").depth(11).exists()) {
            let tfs = className("android.widget.TextView").text("发送").depth(11).findOnce()
            if (tfs != null) {
                log("发送");
                let bounds = tfs.bounds();
                click(bounds.centerX(), bounds.centerY())
                break;
            }
        }

        var bqwg = desc("表情").findOnce();
        if (bqwg != null) {
            log("按表情位置配置");
            let rect = bqwg.bounds();
            log(rect.right,rect.centerY(), rect)
            click(rect.right - 10, rect.centerY() - 110);
            sleep(1000)
            break;
        }

        if (times > 3) {
            log("一直无法点到发送,点击坐标试试");
            back()
            sleep(1000)
            let tfs = text("发送").findOnce()
            if (tfs != null) {
                let bounds = tfs.bounds();
                click(bounds.centerX(), bounds.centerY())
                break;
            }
        }
        var m = className("android.widget.EditText").findOnce();
        if (m != null) {
            click(m.bounds().centerX(), m.bounds().centerY())
        }
        times++;
        sleep(1000);
    }
}
